<?php

namespace app\admin\controller;

use app\admin\business\CashBus;
use app\admin\business\FeedbackBus;
use app\admin\business\OrdersBus;
use app\admin\model\Config as ConfigModel;
use app\admin\model\Goods as goodsModel;
use app\admin\model\OrdersGoods;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use think\db\Where;
use think\facade\Db;
use think\facade\Request;
use think\facade\View;

class Feedback extends Common
{

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 列表
     * @author lijia
     * @date 2019-08-08
     */
    public function index()
    {
        if (Request::isPost()) {
            $key = input('post.key');
            $status = input('post.status',0,'intval');
            $start_time = input('post.start_time');
            $end_time = input('post.end_time');
            $page = input('page') ? input('page') : 1;
            $pageSize = input('limit') ? input('limit') : config('pageSize');
            $sort_by = input('sort_by') ? input('sort_by') : 'id';
            $sort_order = input('sort_order') ? input('sort_order') : 'desc';
            $where = [];
            if ($key) {
                $where[] = ['b.username|b.mobile', 'like', "%" . $key . "%"];
            }
            if ($start_time){
                $where[] = ['a.create_time','>',$start_time];
            }
            if ($end_time){
                $where[] = ['a.create_time','<',$end_time];
            }
            if ($status) {
                $where[] = ['a.name', '=', $status];
            }
            $list = FeedbackBus::getList($where,$sort_by,$sort_order,$pageSize,$page);
            return $result = ['code' => 0, 'msg' => '获取成功!', 'data' => $list['data'], 'count' => $list['total'], 'rel' => 1];
        }
        return View::fetch();
    }



    /**
     * 删除
     * @return Array|array
     */
    public function doDelete()
    {
        try {
            $id = input('request.id');
            if (!$id) {
                return ['code' => 3000, 'msg' => '请选择要删除的数据!'];
            }
            $result = Db::name('feedback')->where("id", $id)->delete();
            if ($result) {
                return ['code' => 0, 'msg' => '操作成功!'];
            }
            return ['code' => 3000, 'msg' => '操作失败!'];
        } catch (\Exception $e) {
            return ['code' => 99999, 'msg' => $e->getMessage()];
        }
    }


    /**
     * 查看
     */
    public function detail()
    {
        try {
            $id = input('id', 0, 'intval');
            if (!$id) {
                return ['code' => 1, 'msg' => '非法请求!'];
            }
            $info = Db::name('feedback')->alias('a')
                ->join('user b','b.user_id = a.user_id','left')
                ->where('a.id',$id)
                ->field('a.*,b.username,b.mobile,b.realname')
                ->find();
            $this->assign('info', $info);
            return View::fetch();
        } catch (\Exception $e) {
            return ['code' => 99999, 'msg' => $e->getMessage()];
        }
    }


    public function getImages(){
        $id = input('param.id');
        $images = Db::name('feedback')->where('id',$id)->value('images');
        $arr = explode(',', $images);
        return json_encode($arr,true);

    }
}